// @ts-ignore
/* eslint-disable */
import * as loginService from '@/services/login';

export interface ModelType {
    namespace: string;
    state: Object;
    effects: {
        query: any;
    };
    reducers: {
        // 启用 immer 之后
        save: any;
    };
    subscriptions: { setup: any };
}

const IndexModel: ModelType = {
    namespace: 'login',

    state: {
        name: '',
    },

    effects: {
        * login({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.login, payload);
            if (callback) {
                callback(response)
            }
        },
        * outLogin({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.outLogin, payload);
            if (callback) {
                callback(response)
            }
        },
        * sendYzm({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.sendYzm, payload);
            if (callback) {
                callback(response)
            }
        },
        * loginMobilePhoneEncrypted({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.loginMobilePhoneEncrypted, payload);
            if (callback) {
                callback(response)
            }
        },

        * sendModifyYzm({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.sendModifyYzm, payload);
            if (callback) {
                callback(response)
            }
        },
        * forgetPwd({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.forgetPwd, payload);
            if (callback) {
                callback(response)
            }
        },
        * updatePwd({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.updatePwd, payload);
            if (callback) {
                callback(response)
            }
        },
        * getQrCode({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.getQrCode, payload);
            if (callback) {
                callback(response)
            }
        },
        * getSchAdminChooseRole({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.getSchAdminChooseRole, payload);
            if (callback) {
                callback(response)
            }
        },
        * updateSchUserToken({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.updateSchUserToken, payload);
            if (callback) {
                callback(response)
            }
        },
        * checkChooseRole({ payload, callback }: any, { call }: any) {
            const response = yield call(loginService.checkChooseRole, payload);
            if (callback) {
                callback(response)
            }
        },
    },
    reducers: {
        // save(state, action) {
        //   return {
        //     ...state,
        //     ...action.payload,
        //   };
        // },
        // 启用 immer 之后
        save(state, action) {
            state.name = action.payload;
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathname === '/') {
                    dispatch({
                        type: 'query',
                    });
                }
            });
        },
    },
};

export default IndexModel;